<script setup lang="ts">
import type { Message } from '@/types/room'
import { IllnessTime, MsgType } from '@/enums/index'
import { showImagePreview } from 'vant'
import { useUserStore } from '@/stores'
import dayjs from 'dayjs'
import { getPrescriptionPic } from '@/services/consult'

const userStore = useUserStore()
defineProps<{
  list: Message[]
}>()

const timeOptions = [
  { label: '一周内', value: IllnessTime.Week },
  { label: '一月内', value: IllnessTime.Month },
  { label: '半年内', value: IllnessTime.HalfYear },
  { label: '大于半年', value: IllnessTime.More }
]
const flagOptions = [
  { label: '就诊过', value: 0 },
  { label: '没就诊过', value: 1 }
]

const getIllnessTimeText = (time?: IllnessTime) => {
  return timeOptions.find((item) => item.value === time)?.label
}

const getConsultFlagText = (flag?: 0 | 1) => {
  return flagOptions.find((item) => item.value === flag)?.label
}

// 图片预览
const previewImg = (pictures?: any[]) => {
  if (pictures && pictures.length) showImagePreview(pictures.map((item) => item.url))
}

// 时间格式化
const forMatTime = (time: string) => {
  return dayjs(time).format('HH:mm')
}

// 图片滚动到最底部
const loadSuccess = (notScorll: boolean) => {
  // 判断时聊天记录的图片则不去滚动
  if (notScorll === true) return
  window.scrollTo(0, document.body.scrollHeight)
}

// 点击查看电子处方
const showPrescription = async (id: string) => {
  const res = await getPrescriptionPic(id)
  showImagePreview([res.data.url])
}
</script>

<template>
  <template v-for="{ id, msgType, msg, from, createTime, notScorll } in list" :key="id">
    <!-- 病情描述卡片 -->
    <div class="msg msg-illness" v-if="msgType === MsgType.CardPat">
      <div class="patient van-hairline--bottom">
        <p>
          {{ msg.consultRecord?.patientInfo.name }}
          {{ msg.consultRecord?.patientInfo.genderValue }}
          {{ msg.consultRecord?.patientInfo.age }}岁
        </p>
        <p>
          {{ getIllnessTimeText(msg.consultRecord?.illnessTime) }} |
          {{ getConsultFlagText(msg.consultRecord?.consultFlag) }}
        </p>
      </div>
      <van-row>
        <van-col span="6">病情描述</van-col>
        <van-col span="18">{{ msg.consultRecord?.illnessDesc }}</van-col>
        <van-col span="6">图片</van-col>
        <van-col span="18" @click="previewImg(msg.consultRecord?.pictures)">点击查看</van-col>
      </van-row>
    </div>
    <!-- 温馨提示-通知 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.NotifyTip">
      <div class="content">
        <span class="green">温馨提示：</span>
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 通知 -->
    <div class="msg msg-tip" v-if="msgType === MsgType.Notify">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
    <!-- 发送-文字 -->
    <div class="msg msg-to" v-if="msgType === MsgType.MsgText && from === userStore.user?.id">
      <div class="content">
        <div class="time">{{ forMatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
      <van-image src="https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/popular_3.jpg" />
    </div>
    <!-- 发送-图片 -->
    <div class="msg msg-to" v-if="msgType === MsgType.MsgImage && from === userStore.user?.id">
      <div class="content">
        <div class="time">{{ forMatTime(createTime) }}</div>
        <van-image @load="loadSuccess(notScorll)" fit="contain" :src="msg.picture?.url" />
      </div>
      <van-image src="https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/popular_3.jpg" />
    </div>
    <!-- 接收-文字 -->
    <div class="msg msg-from" v-if="msgType === MsgType.MsgText && from !== userStore.user?.id">
      <van-image src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" />
      <div class="content">
        <div class="time">{{ forMatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
    </div>
    <!-- 收消息-图片 -->
    <div class="msg msg-from" v-if="msgType === MsgType.MsgImage && from !== userStore.user?.id">
      <van-image src="https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg" />
      <div class="content">
        <div class="time">{{ forMatTime(createTime) }}</div>
        <van-image @load="loadSuccess(notScorll)" fit="contain" :src="msg.picture?.url" />
      </div>
    </div>
    <!-- 处方消息 -->
    <div class="msg msg-recipe" v-if="msgType === MsgType.CardPre">
      <div class="content">
        <div class="head van-hairline--bottom">
          <div class="head-tit">
            <h3>电子处方</h3>
            <p @click="showPrescription(msg.prescription?.id)">
              原始处方 <van-icon name="arrow"></van-icon>
            </p>
          </div>
          <p>
            {{ msg.prescription?.name }} {{ msg.prescription?.genderValue }}
            {{ msg.prescription?.age }}岁 {{ msg.prescription?.diagnosis }}
          </p>
          <p>开方时间：{{ msg.prescription?.createTime }}</p>
        </div>
        <div class="body">
          <div class="body-item" v-for="item in msg.prescription?.medicines" :key="item.id">
            <div class="durg">
              <p>{{ item.name }} {{ item.specs }}</p>
              <p>{{ item.usageDosag }}</p>
            </div>
            <div class="num">x{{ item.quantity }}</div>
          </div>
        </div>
        <div class="foot"><span>购买药品</span></div>
      </div>
    </div>
    <!-- 订单取消 -->
    <!-- <div class="msg msg-tip msg-tip-cancel">
      <div class="content">
        <span>订单取消</span>
      </div>
    </div> -->
  </template>
</template>

<style lang="scss" scoped>
@import '@/styles/room.scss';
</style>
